package com.liferay.controller;

import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.contant.Contants;
import com.liferay.model.Faculty;
import com.liferay.model.Users;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.service.impl.EditUserServiceImpl;


@Controller(value = "EditUserController")
@RequestMapping("VIEW")
public class EditUserController {
	private static Logger log = Logger.getLogger(EditUserController.class);
	private EditUserServiceImpl editUserService;
	private long fkUser=0;
	private long keepfkFaculty=0;

	@RenderMapping
	public String handleRenderRequest(RenderRequest request, RenderResponse response, Model model) throws NumberFormatException, SystemException{
		log.info("show view default EditUserController");
		editUserService = new EditUserServiceImpl();
		List<Users> listUser =  editUserService.getListUser();
		model.addAttribute("listUser", listUser);
		return "view";
	}
	
	@RenderMapping(params="action=default")
	public String handleRenderRequest1(RenderRequest request, RenderResponse response, Model model) throws NumberFormatException, SystemException{
		log.info("show view default EditUserController");
		editUserService = new EditUserServiceImpl();
		List<Users> listUser =  editUserService.getListUser();
		model.addAttribute("listUser", listUser);
		return "view";
	}
	
	@RenderMapping(params="action=error")
	public String handleRenderRequest2(RenderRequest request, RenderResponse response){
		return "error";
	}

	@ActionMapping(params = "action=edituser")
	public void getStudentID(ActionRequest request, ActionResponse response) {
		fkUser = ParamUtil.getLong(request, "fkUser");
		keepfkFaculty = ParamUtil.getLong(request, "fkFaculty");
		response.setRenderParameter("action", "renderedituser");
	}

	@RenderMapping(params="action=renderedituser")
	public String renderedituser(RenderRequest request, RenderResponse response, Model model) throws NumberFormatException, SystemException{
		log.info("show view default renderedituser");
		editUserService = new EditUserServiceImpl();
		List<Faculty> listFaculty = editUserService.getListFaculty();
		model.addAttribute("listFaculty", listFaculty);
		model.addAttribute("keepfkFaculty", keepfkFaculty);
		return "renderedit";
	}
	
	@ActionMapping(params = "action=saveinformationedit")
	public void saveinformationEdit(ActionRequest request, ActionResponse response) throws NumberFormatException, SystemException {
		long fkFaculty = ParamUtil.getLong(request, "fkFaculty");
		editUserService = new EditUserServiceImpl();
		Users user = new Users();
		user.setFkFaculty(fkFaculty);
		user.setIdUser(fkUser);
		if(editUserService.updateInformation(user).equals(Contants.SUCCESS)){
			fkUser=0;
			response.setRenderParameter("action", "default");
		}
		else{
			response.setRenderParameter("action", "error");
		}
	}
}
